import {toPlainObjectDeep} from '../../../utils/toPlainObjectDeep';
import {observable, action} from 'mobx';
import {PageBean, PageSearchBean, PageController} from '../../../model/page/Page';
import {AdAppPositionController} from '../../../model/ad/adPosition/AdAppPositionController'
import _ from 'lodash';
import {FormStore, ListStore, FieldItem, isRequired, isSelectRequired, toJSDeep} from '../../common/store/Store';
import {message} from 'antd';

const pageController = new PageController();
const adAppPositionController = new AdAppPositionController();

export class PageFields implements PageBean<FieldItem> {
    constructor(type = 'add'){
        if(type === 'edit'){
            _.forEach(this, (field:FieldItem, key: string) => {
                if(field.hasFeedback === true){
                    field.validateStatus = 'success';
                }
            })  
        }
    }

    id: FieldItem = new FieldItem({
        value: null
    }); 
    pageId: FieldItem = new FieldItem({
        value: null
    }); 
    description: FieldItem = new FieldItem({
        value: ''
    });
    name: FieldItem = new FieldItem({
        value: '',
        hasFeedback: true,
        required: true
    });
    title: FieldItem = new FieldItem({
        value: '',
        hasFeedback: true,
        required: true
    });
    buId: FieldItem = new FieldItem({
        value: -1,
        hasFeedback: true,
        required: true,
        disabled:false,
        option:[{
            id: -1,
            name: '请选择',
            title:'请选择',
            description:'请选择'
        }]
    });
    appId: FieldItem = new FieldItem({
        value: -1,
        hasFeedback: true,
        required: true,
        disabled:true,
        option:[{
            id: -1,
            name: '请选择',
            title:'请选择',
            description:'请选择'
        }]
    });
    platformType: FieldItem = new FieldItem({
        value: -1,
        hasFeedback: true,
        required: true,
        disabled:true,
        option:[{
            type: -1,
            name: '请选择',
            title:'请选择',
            description:'请选择'
        }]
    });
}

export class PageSearchFields implements PageSearchBean<FieldItem>{
    description: FieldItem = new FieldItem({
        value: ''
    });
    title: FieldItem = new FieldItem({
        value: ''
    });
}

const addDefaultFields = toPlainObjectDeep(new PageFields());
const editDefaultFields = toPlainObjectDeep(new PageFields('edit'));

export class PageBaseFormStore extends FormStore<PageFields, PageBean<any>> {
    @observable confirmLoading:boolean = false;

    @action changeConfirmLoading(params){
        this.confirmLoading = params;
    }

    validators = {
        title: [isRequired],
        name: [isRequired],
        buId:[isSelectRequired],
        appId:[isSelectRequired],
        platformType:[isSelectRequired]
    };

    /*==================级联菜单开始==========================*/
    @action getBu(partnerId = null){
        return adAppPositionController
            .getPartnerBu({partnerId: partnerId})
            .then((data)=>{
                this.fields.buId.option = _.cloneDeep(toJSDeep(this.fields.buId.option)).concat(data.result);
                return data;
            });
    }

    @action getApp(buId = null){
        return adAppPositionController
            .getPartnerApps({BUId: buId})
            .then((data)=>{
                this.fields.appId.option = _.cloneDeep(toJSDeep(this.fields.appId.option)).concat(data.result);
                return data;
            });
    }

    @action getPlatForm(buId = null,appId = null){
        return adAppPositionController
            .getPartnerAppPlatforms({BUId: buId, appId: appId})
            .then((data)=>{
                this.fields.platformType.option = _.cloneDeep(toJSDeep(this.fields.platformType.option)).concat(data.result);
                return data;
            });
    }

    @action selectBu(){
        this.clearApp();
        this.clearPlatForm();
        if (_.toString(this.fields.buId.value) !== '-1') {
            return this.getApp(this.fields.buId.value).then(() => {
                this.changeField('appId',{disabled:false})
            });
        }
    }

    @action selectApp(){
        this.clearPlatForm();
        if (_.toString(this.fields.appId.value) !== '-1') {
            return this.getPlatForm(this.fields.buId.value,this.fields.appId.value).then(() => {
                this.changeField('platformType',{disabled:false})
            });
        }
    }

    @action selectPlatForm(){
        return new Promise((get) => {
            get(this.fields.platformType.value);
        })
    }

    @action clearBu(){
        this.fields.buId = _.cloneDeep(this.defaultFields.buId);
    }

    @action clearApp(){
        this.fields.appId = _.cloneDeep(this.defaultFields.appId);
        this.valid('appId',this.fields.appId.value.toString());
    }

    @action clearPlatForm(){
        this.fields.platformType = _.cloneDeep(this.defaultFields.platformType);
        this.valid('platformType',this.fields.platformType.value.toString());
    }

    @action clearSelectAll(){
        this.clearBu();
        this.clearApp();
        this.clearPlatForm();
    }

    @action
    bindData(data:PageBean<any>){
        this.changeFields({
            buId: { disabled:true },
            title: { disabled:true },
            name: { disabled:true }
        })
        this.getBu();
        this.getApp(data.buId);
        this.getPlatForm(data.buId,data.appId);
    }
    /*==================级联菜单结束==========================*/
}

export class Add extends PageBaseFormStore {
    defaultFields = _.cloneDeep(addDefaultFields);

    @observable fields: PageFields = _.cloneDeep(addDefaultFields);

    @action load(){
        this.getBu();
        addStore.show();
    }

    @action addPage(params:PageBean<any>){
        return pageController.addPage(params);
    }
}

export class Edit extends PageBaseFormStore {
    defaultFields = _.cloneDeep(editDefaultFields);

    @observable fields: PageFields = _.cloneDeep(editDefaultFields);

    @action load(id: number) {
        pageController.getPage({id:id}).then((res) => {
            if(res.success){
                let data = res.result;
                this.bindData(_.cloneDeep(data));
                this.show(data);
            }else{
                message.error(res.message,3);
            }
        })
    }

    @action editPage(params:PageBean<any>){
        return pageController.editPage(params);
    }
}

class Check extends PageBaseFormStore {
    @observable fields: PageFields = _.cloneDeep(addDefaultFields);
}

export class List extends ListStore<PageBean<any>> {
    @action searchPage(params?:any){
        this._SpiLoading = true;

        pageController.searchPage(params).then((data)=>{
            this.searchCallback(data);
        })
    }

    @action delete(id: number){
        return pageController.deletePage({id:id});
    }

    @action load(params:any = {}){
        this.searchPage(params);
    }

    @action refresh(search: Search){
        this.changeState(true);
        search.clearAll();
        this.load();
    }
}

const searchDefaultFields = toPlainObjectDeep(new PageSearchFields());

class Search extends FormStore<PageSearchFields,PageSearchBean<any>>{
    
    defaultFields = _.cloneDeep(searchDefaultFields);

    @observable fields: PageSearchFields = _.cloneDeep(searchDefaultFields);

    @observable expand:boolean = false;

    @action OnExpand(expand){
        this.expand = !expand;
    }

    @action clearSearch(list:List){
        this.clearAll();
        list.load();
    }

    @action clearSearchState(){
        this.expand = false;
        this.clearAll();
    }
}

export const listStore = new List();
export const addStore = new Add();
export const editStore = new Edit();
export const checkStore = new Check();
export const searchStore = new Search();